import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form---多选、照片
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/HntManage/pouringOrder/WBSHuitanGangbao/index.vue";

import {
    saveFieldtestCheckWitness, //保存
    getFieldtestCheckWitnessByFid, //详情
    getBaseSgCompanyList, //受检单位
    getBaseDataitemDetailByItemCode, //字典项(设计强度等级--Qdlevel)
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getBaseDepartmentUser, //选择人
    getFieldtestCheckWitnessByCompany // 根据受检单位查询编号/其他单位接口
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "radioTypes"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            WBSShow: false,
            fTestnoteIndex: null, //检测依据
            fJudgetypeIndex: null, //判定依据
            selected: [],


            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: [],
            additionalObj: formJs.additionalObj,
            isEdit: false,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "",
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目简称
                fEngineeringname: "", //工程名称
                fWtnumber: "", //委托单编号
                fWtdates: "", //委托日期
                fWtunitid: "", //委托单位id
                fWtunit: "", //委托单位
                fWtunituser: "", //委托人
                fWtunituserid: "",
                fWtphone: "", //委托人联系方式
                fCompanyid: "", //受检单位id
                fCompanyname: "", //受检单位
                fJzunit: "", //见证单位
                fJzuser: "", //见证人
                fJzphone: "", //见证人联系方式
                fEngineeringpartid: "", //工程部位id
                fEngineeringpartname: "", //工程部位
                fChecktype: "", //检测类别

                fSpecimenage: "", //龄期
                fConcretetype: "", //是否泵送混凝土
                fThicknessdesignvalue: "", //设计厚度
                fAllowdeviation: "", //允许偏差（-mm）
                fAllowdeviation1: "", //允许偏差（+mm）

                fSjunit: "", //设计单位
                fJsunit: "", //建设单位
                fJsunitid: "", //建设单位id
                fJlunit: "", //监理单位
                fJlunitid: "", //监理单位id
                fSgunit: "", //施工单位
                fSgunitid: "", //施工单位id
                fJcunit: "", //检测单位
                fJcunitid: "", //检测单位id
                fPiletype: "", //桩基类型
                fPiletopdesignhigh: "", //设计桩顶标高
                fPiletopactualhigh: "", //实际桩顶标高
                fPilebottomdesignhigh: "", //设计桩底标高
                fPilebottomactualhigh: "", //实际桩底标高
                fPiledesigndiameter: "", //设计桩径
                fPilelongsoundhigh: "", //声测管管长
                fPiletopsoundhigh: "", //声测管顶标高（M）
                fPilebottomsoundhigh: "", //声测管底标高（M）
                fPouringdates: "", //浇筑日期
                fTestdates: "", //要求检测日期
                fSguser: "", //施工单位负责人id
                fSguserid: "", //施工单位负责人
                fJluser: "", //监理单位负责人id
                fJluserid: "", //监理单位负责人
                fJcuser: "", //检测单位接收人id
                fJcuserid: "", //检测单位接收人
                fJcuseuser: "", //检测单位联系人id
                fJcuseuserid: "", //检测单位联系人

                fConcretelevel: "", //设计强度
                fTestresult: "", //检测参数
                fTestprice: "", //检测费用
                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fCheckfile: "", //原始资料附件
                fNote: "", //备注
                fStatus: "", //检测状态
                recordfiles: [], //删除文件

                fNature: "", // 当前登录用户的机构性质
                userId: "", //当前登录人id
                userName: "" //当前登录人姓名
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},
            fCompanynames: null,
            fCompanyids: null,
            fileList: [], //附件
        };
    },
    components: {
        publicForm,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal
                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },
        radioTypes: {
            handler(newVal) {
                console.log(1111, newVal);
                if (newVal) {
                    this.types = newVal;

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        
        this.btnShow = true;
        this.formData.fProjectid = this.userInfo.projectId; //项目简称id
        this.formData.fProjectname = this.userInfo.projectFullName; //项目简称

        this.formData.fWtunit = this.userInfo.companyFullName; //委托单位
        this.formData.fWtunitid = this.userInfo.companyId; //委托单位

        this.formData.fWtunituser = this.userInfo.realName; //委托人
        this.formData.fWtunituserid = this.userInfo.userId; //委托人
        this.formData.fWtphone = this.userInfo.mobile; //委托人联系方式

        this.formData.fCheckfile = 'fCheckfile' + Date.now();
        if (this.types == 0) {
            this.formObj = formJs.formObj;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fChecktype = "回弹检测";

            this.getCheckitemsList("3"); //回弹试验依据
            this.getCheckitemsList("4"); //回弹判定依据

            this.formData.fWtdates = this.common.day(0); //委托日期


        } else if (this.types == 1) {

            this.formObj = formJs.formObj1;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fChecktype = "钢筋保护层检测";
            this.getCheckitemsList("5"); //钢保试验依据
            this.getCheckitemsList("6"); //钢保判定依据
            this.formData.fWtdates = this.common.day(0); //委托日期

        } else if (this.types == 2) {
            this.formObj = formJs.formObj2;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fChecktype = "桩基检测";
            this.getCheckitemsList("7"); //桩基试验依据
            this.getCheckitemsList("8"); //桩基判定依据
            this.formData.fPouringdates = this.common.day(0); //委托日期
            this.formData.fTestdates = this.common.day(0); //委托日期
            this.formData.fWtdates = this.common.day(0); //委托日期


        }else if (this.types == 3) {
            this.formObj = formJs.formObj2;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fChecktype = "成孔质量检测";
            this.getCheckitemsList("7"); //桩基试验依据
            this.getCheckitemsList("8"); //桩基判定依据
            this.formData.fPouringdates = this.common.day(0); //委托日期
            this.formData.fTestdates = this.common.day(0); //委托日期
            this.formData.fWtdates = this.common.day(0); //委托日期


        }
        
        console.log('this.statusNum', this.statusNum);
        if (this.statusNum == 1) { //新增
            // this.formData = this.$options.data.call(this).formData;
            this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
            this.getBaseTree();
            // this.formObj.map((item) => {
            //     item.disabled = false;
            // });
            this.formObj[1].disabled = true;
            this.formObj[11].disabled = true;
            this.formObj[10].inputButton = true;
            if (this.types == 0) {
                this.formObj[19].uploadSelect = true
            } else if (this.types == 1) {
                this.formObj[21].uploadSelect = true
            } else if (this.types == 2) {
                this.formObj[13].disabled = true;
                this.formObj[14].disabled = true;
                this.formObj[15].disabled = true;
                this.formObj[37].uploadSelect = true
            }

        } else if (this.statusNum == 2) { //编辑
            this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
            this.getBaseTree();
            this.formData.fId = this.currentDetail.fId //按钮 

            if (this.currentDetail.fChecktype == '回弹检测') {
                this.formObj = formJs.formObj;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[1].disabled = true;
                this.formObj[11].disabled = true;
                this.formObj[10].inputButton = true;
                this.formObj[19].uploadSelect = true
                this.formData.fChecktype = "回弹检测";

                this.getCheckitemsList("3"); //回弹试验依据
                this.getCheckitemsList("4"); //回弹判定依据
            } else if (this.currentDetail.fChecktype == '钢筋保护层检测') {
                this.formObj = formJs.formObj1;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[1].disabled = true;
                this.formObj[11].disabled = true;
                this.formObj[10].inputButton = true;
                this.formData.fChecktype = "钢筋保护层检测";
                this.getCheckitemsList("5"); //钢保试验依据
                this.getCheckitemsList("6"); //钢保判定依据
                this.formObj[21].uploadSelect = true
            } else if (this.currentDetail.fChecktype == '桩基检测') {
                this.formObj = formJs.formObj2;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formData.fChecktype = "桩基检测";
                this.getCheckitemsList("7"); //桩基试验依据
                this.getCheckitemsList("8"); //桩基判定依据
                this.formObj[1].disabled = true;
                this.formObj[11].disabled = true;
                this.formObj[10].inputButton = true;
                this.formObj[13].disabled = true;
                this.formObj[14].disabled = true;
                this.formObj[15].disabled = true;
                this.formObj[37].uploadSelect = true
            }else if (this.currentDetail.fChecktype == '成孔质量检测') {
                this.formObj = formJs.formObj2;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formData.fChecktype = "成孔质量检测";
                this.getCheckitemsList("7"); //桩基试验依据
                this.getCheckitemsList("8"); //桩基判定依据
                this.formObj[1].disabled = true;
                this.formObj[11].disabled = true;
                this.formObj[10].inputButton = true;
                this.formObj[13].disabled = true;
                this.formObj[14].disabled = true;
                this.formObj[15].disabled = true;
                this.formObj[37].uploadSelect = true
            }


            // this.getDetail({
            //     fId: this.currentDetail.fId
            // })

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            

            if (this.currentDetail.fChecktype == '回弹检测') {
                this.formObj = formJs.formObj;
        
                this.formObj[10].inputButton = false;
                this.formObj[19].uploadSelect = false;
            } else if (this.currentDetail.fChecktype == '钢筋保护层检测') {
                this.formObj = formJs.formObj1;

                this.formObj[10].inputButton = false;
                this.formObj[21].uploadSelect = false;
            } else if (this.currentDetail.fChecktype == '桩基检测') {
                this.formObj = formJs.formObj2;
             
                this.formObj[10].inputButton = false;
                this.formObj[37].uploadSelect = false;
            }

            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.formObj[6].options = [];
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (b == "fCompanyname") {
                this.formObj[6].options.map(item => {
                    if (data == item.label) {
                        this.formData.fCompanyname = item.label;
                        this.formData.fCompanyid = item.value;
                        this.getEntrustno();
                    }
                })
            }
            if (b == 'fTestnote1') {
                var arr = []
                data.map(item => {
                    if (item.label) {
                        arr.push(item.label)
                    } else {
                        arr.push(item)
                    }
                })
                this.formData.fTestnote = arr.join(",");
            } else if (b == 'fJudgetype1') {
                var arr1 = []
                data.map(item => {
                    if (item.label) {
                        arr1.push(item.label)
                    } else {
                        arr1.push(item)
                    }

                })
                this.formData.fJudgetype = arr1.join(",")
                // this.formData.fJudgetype1 = arr1
            } 
            else if (b == "fSguser") { //施工单位负责人


                this.formData.fSguser = data.label; //施工单位负责人
                this.formData.fSguserid = data.value //施工单位负责人id
            } 
            else if (b == "fJluser") { //监理单位负责人
                this.formData.fJluser = data.label; //监理单位负责人
                this.formData.fJluserid = data.value //监理单位负责人id
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        refreshList() {},
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        clickDialog(index, prop) {

            // 取样--工程部位
            if (prop == "fEngineeringpartname") {
                this.WBSShow = true;
            }

            return index, prop;
        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fEngineeringpartname = data.fLongname //工程部位
            this.formData.fEngineeringpartid = data.fNodeid //工程部位id
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                var data = res.data.baseDataitemdetailList;
                this.formObj.map((item, index) => {
                    if (item.prop == 'fConcretelevel') {
                        console.log(index, 88);

                        this.formObj[index].options = [];
                        data.map(item => {
                            this.formObj[index].options.push({
                                value: item.fItemdetailid,
                                label: item.fItemvalue
                            })
                        })
                    }
                })

            })
        },
        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                    modelType: num,
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        var num1; // 检测依据
                        var num2; //判定依据
                        this.formObj.map((item, index) => {

                            // 检测依据
                            if (item.prop == 'fTestnote1') {
                                num1 = index;
                                this.fTestnoteIndex = index;
                            } else if (item.prop == 'fJudgetype1') {
                                num2 = index;
                                this.fJudgetypeIndex = index;

                            }
                        })
                        if (num == 3 || num == 5 || num == 7) {
                            this.formObj[num1].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[num1].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        } else if (num == 4 || num == 6 || num == 8) {
                            this.formObj[num2].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[num2].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        }

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        //获取委托单编号
        getEntrustno() {
            var data = {
                fCompanyid: this.formData.fCompanyid, //受检单位
                fWtdates: this.formData.fWtdates, //委托时间  不传默认当前日期
            }
            getFieldtestCheckWitnessByCompany(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data;
                        this.formData.fWtnumber = data.fWtnumber; //委托编号

                        if (this.types == 2) {
                            this.formData.fJsunit = data.fJsunit; //建设单位
                            this.formData.fJsunitid = data.fJsunitid; //建设单位id
                            this.formData.fJlunit = data.fJlunit; //监理单位
                            this.formData.fJlunitid = data.fJlunitid; //监理单位id
                            this.formData.fSgunit = data.fSgunit; //施工单位
                            this.formData.fSgunitid = data.fSgunitid; //施工单位id
                            this.formData.fJcunit = data.fJcunit; //检测单位
                            this.formData.fJcunitid = data.fJcunitid; //检测单位id
                            this.getPerson(data.fSgunitid, 0) //施工单位负责人
                            this.getPerson(data.fJlunitid, 1) //监理单位负责人

                        }




                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        //查受检单位
        getBaseTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
           
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;

                        data.map(item => {
                            this.formObj[6].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        // this.formData.fCompanyname = this.formObj[0].options[0].label;
                        // this.formData.fCompanyid = this.formObj[0].options[0].value;
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        getPerson(id, status) {
            var obj = {
                fCompanyid: id,
            };
            this.formObj[28].options = [];
            this.formObj[29].options = [];
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        var arr = res.data.childList[0].personList;
                        var personList = [];

                        arr.map((item) => {
                            personList.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });

                        });



                        if (status == 0) { //施工单位负责人
                            this.formObj[28].options = personList;

                        } else if (status == 1) { //监理单位负责人
                            this.formObj[29].options = personList;

                        }
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })

        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getFieldtestCheckWitnessByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpCheckFieldtestwitness;
                    this.formData = data;
                    this.formData.fFileurl = data.fFileurl;
                    this.formData.recordfiles=[];
                    this.fileList = res.data.fFilelist;
                    if (data.fChecktype == "桩基检测") {

                        this.getPerson(data.fSgunitid, 0) //施工单位负责人
                        this.getPerson(data.fJlunitid, 1) //监理单位负责人

                    }else if (data.fChecktype == "成孔质量检测") {

                        this.getPerson(data.fSgunitid, 0) //施工单位负责人
                        this.getPerson(data.fJlunitid, 1) //监理单位负责人

                    }


                    //    检测依据
                    if (data.fTestnote != '') {
                        var fTestnote = []
                        this.formObj[this.fTestnoteIndex].options.map(item => {
                            data.fTestnote.split(",").map(i => {
                                if (item.label == i) {
                                    fTestnote.push(item)
                                }
                            })
                        })

                    } else {
                        fTestnote = []
                    }

                    if (data.fJudgetype != '') {
                        var fJudgetype = []
                        this.formObj[this.fJudgetypeIndex].options.map(item => {
                            data.fJudgetype.split(",").map(i => {
                                if (item.label == i) {
                                    fJudgetype.push(item)
                                }
                            })
                        })

                    } else {
                        fJudgetype = []
                    }

                    this.selected = [{
                            name: "fTestnote1",
                            selected: fTestnote,
                        },
                        {
                            name: "fJudgetype1",
                            selected: fJudgetype,
                        },

                    ];

                    // if (this.statusNum == 1) {
                    //     this.formData.recordfiles = [];
                    //     console.log(this.userInfo.userId);
                    //     // 委托人
                    //     if (data.fCommissionid == null || data.fCommissionid == "" || data.fCommissionname == null || data.fCommissionname == "") {
                    //         this.formData.fCommissionid = this.userInfo.userId;
                    //         this.formData.fCommissionname = this.userInfo.realName;
                    //     }
                    //     // 委托日期
                    //     if (data.fCommissiondates == null || data.fCommissiondates == "") {
                    //         this.formData.fCommissiondates = this.common.day(0)
                    //     }
                    // }


                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }

            });
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fNature = this.userInfo.companyNature;
            console.log(this.formData);
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            // var data = {
            //     fId: obj.fId, //必传
            //     userId: obj.userId, //当前登录人id
            //     userName: obj.userName, //当前登录人姓名
            //     recordfiles: obj.recordfiles, //删除文件id集合
            //     fCommissiondates: obj.fCommissiondates, //委托日期
            //     fCommissionname: obj.fCommissionname, //委托人
            //     fCommissionid: obj.fCommissionid, //委托人id
            //     fUnitname: obj.fUnitname, //接收委托单位名称
            //     fUnitid: obj.fUnitid, //接收委托单位id
            //     fOrdernumber: obj.fOrdernumber, //委托单号
            //     fFileurl: obj.fFileurl, //委托单附件
            //     fNote: obj.fNote, //备注
            //     fCompanyid: obj.fCompanyid,
            //     fChecktype: obj.fChecktype,
            // }
            this.loading = true;
            this.isLock = true
            saveFieldtestCheckWitness(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};